//
//  DataSheet.m
//  waterland
//
//  Created by ericyang on 2011/5/16.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "DataSheet.h"


@implementation DataSheet

- (id)init
{
    self = [super init];
    
    if (self)
    {
        columnCount = 0;
        columnNameList = [[NSMutableArray alloc] init];
        
        rowCount = 0;
        rowDataList = [[NSMutableArray alloc] init];
    }
    
    return self;
}

- (void)dealloc
{
    [columnNameList removeAllObjects];
    [columnNameList release];
    
    [rowDataList removeAllObjects];
    [rowDataList release];
    
    [sheetName release];
    
    [super dealloc];
}

#pragma mark -
#pragma 表單屬性

- (void)setSheetName: (NSString*)name
{
    sheetName = [[NSString alloc] initWithString:name];
}

- (NSString*)getSheetName
{
    return sheetName;
}

#pragma mark -
#pragma 欄位屬性

- (NSInteger)getColumnCount
{
    return columnCount;
}

- (void)addColumnWithColumnName:(NSString*)name
{
    [columnNameList addObject:name];
    ++columnCount;
}

- (NSString*)getColumnWithIndex: (NSInteger)index;
{
    if (index < 0)
        return nil;
    
    if (index >= columnCount)
        return nil;
    
    return [columnNameList objectAtIndex:index];
}

#pragma mark -
#pragma 資料欄位

- (NSInteger)createRow
{
    NSMutableArray* rowData = [[NSMutableArray alloc] init];
    [rowDataList addObject:rowData];
    
    [rowData release];
    
    rowCount++;
    
    return (rowCount - 1);
}

- (void)addRowDataWithRowIndex:(NSInteger)rowIndex rowData:(NSString*)rowData
{
    if (rowIndex < 0)
        return;
    
    if(rowIndex >= [rowDataList count])
        return;
    
    [[rowDataList objectAtIndex:rowIndex] addObject:rowData];
}

- (NSString*)getRowDataByColumnIndexWithRowIndex:(NSInteger)rowIndex columnIndex:(NSInteger)columnIndex;
{
    if (rowIndex < 0)
        return nil;
    
    if(rowIndex >= [rowDataList count])
        return nil;
    
    if (columnIndex < 0)
        return nil;
    
    if (columnIndex >= columnCount)
        return nil;
    
    return [[rowDataList objectAtIndex:rowIndex] objectAtIndex:columnIndex];
}

- (NSString*)getRowDataByColumnNameWithRowIndex:(NSInteger)rowIndex columnName:(NSString*)columnName
{
    for (NSInteger i = 0; i < columnCount; i++)
    {
        NSString* checkName = [columnNameList objectAtIndex:i];
        
        if ([checkName isEqualToString:columnName])
            return [self getRowDataByColumnIndexWithRowIndex:rowIndex columnIndex:i];
    }
    
    return nil;
}

- (NSInteger)getRowCount
{
    return rowCount;
}

@end
